package com.dji.sample.common.model;

import io.netty.util.HashedWheelTimer;
import io.netty.util.TimerTask;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author yjt
 * @date 2022-09-09
 * @description: 全局缓存模块  CacheModel.get("devicesPositionOSDServiceImpl").name("handleOSD")
 */
public class CacheModel {

    static HashedWheelTimer hashedWheelTimer = new HashedWheelTimer(1,TimeUnit.SECONDS,60);

    public static Map<String, StoreCache> storeCacheMap = new ConcurrentHashMap<>(16);

    public static StoreCache get(String key) {
        if (storeCacheMap.get(key) == null) {
            storeCacheMap.put(key, new StoreCache());
        }
        return storeCacheMap.get(key);
    }

    /**
     * addTask
     * @author yjt
     * @date: 2022/10/19
     * @param timerTask:
     * @param delay: 缓存多长时间 秒
     * @return: void
     */
    public static void addTask(TimerTask timerTask, Long delay){
        hashedWheelTimer.newTimeout(timerTask,delay,TimeUnit.SECONDS);
    }

    public static void removeKeyDelay(String key,String kay2,Long delay){
        TimerTask timerTask = timeout -> {
            if(get(key)==null || get(key).size()==0){
                storeCacheMap.remove(key);
            }else {
                get(key).remove(kay2);
            }

        };
        hashedWheelTimer.newTimeout(timerTask,delay,TimeUnit.SECONDS);
    }

}
